import numpy as np
from numpy import sin,cos,deg2rad,dot,array
from face_lib import combine

def rotate_by_matrix(rotation_matrix):
    def rotate(vertor)->array:
        # 对矩阵进行转置
        transpose_matrix = np.transpose(vertor)
        return np.transpose(dot(rotation_matrix,transpose_matrix))
    return rotate

def x_rotation_matrix(degree):
    radians = deg2rad(degree)

    return np.array([
        [1, 0, 0],
        [0, cos(radians), sin(radians)],
        [0, -sin(radians), cos(radians)],
    ])

def y_rotation_matrix(degree):
    radians = deg2rad(degree)

    return np.array([
        [cos(radians),  0, -sin(radians)],
        [0,             1, 0            ],
        [sin(radians),  0, cos(radians)],
    ])

def z_rotation_matrix(degree):
    radians = deg2rad(degree)

    return np.array([
        [cos(radians),  sin(radians), 0 ],
        [-sin(radians), cos(radians), 0 ],
        [0,             0,            1 ],
    ])

def rotate_around_x(degree):
    # 绕世界坐标系的 x 轴旋转
    return rotate_by_matrix(x_rotation_matrix(degree))

def rotate_around_y(degree):
    # 绕世界坐标系的 y 轴旋转
    return rotate_by_matrix(y_rotation_matrix(degree))

def rotate_around_z(degree):
    # 绕世界坐标系的 z 轴旋转
    return rotate_by_matrix(z_rotation_matrix(degree))

def rotation_degrees_to_rotation_matrix(rotation_degree_x ,rotation_degree_y ,rotation_degree_z):
    """
        把围绕三轴的旋转角度转换为三个旋转矩阵相互点积之后合并为一个旋转矩阵
    """
    rotation_matrix_x = x_rotation_matrix(rotation_degree_x)
    rotation_matrix_y = y_rotation_matrix(rotation_degree_y)
    rotation_matrix_z = z_rotation_matrix(rotation_degree_z)
    
    return dot(dot(rotation_matrix_z,rotation_matrix_y),rotation_matrix_x)

def rotate_3_axises_around_the_world(rotation_degree_x ,rotation_degree_y ,rotation_degree_z):

    rotation_matrix = rotation_degrees_to_rotation_matrix(rotation_degree_x ,rotation_degree_y ,rotation_degree_z)

    return rotate_by_matrix(rotation_matrix)


# rotated = rotate_around_z(90)(np.array([1,1,0]))
# print(rotated)